<template>
  <div class="dashboard-container">
    <div class="app-container">
      <el-card shadow="never">
        <span class="span">学科名称</span>
        <el-input v-model="subjectName" class="input"></el-input>
        <el-button class="button" @click="clear">清除</el-button>
        <el-button type="primary" @click="search">搜索</el-button>
        <el-button type="success" @click="dialogVisible = true" class="Dialog el-icon-edit"> 新增学科</el-button>
        <el-dialog title="新增学科" :visible="dialogVisible" width="40%" height="30%" :before-close="handleClose">
          <el-form :model="form" :rules="rules" ref="ruleForm" label-width="100px" class="demo-ruleForm">
            <el-form-item label="学科名称" prop="subjectName">
              <el-input v-model="form.subjectName" class="input1" placeholder="请输入学科名称"></el-input>
            </el-form-item>
            <el-form-item label="是否显示">
              <el-switch class="switch" v-model="value" active-color="#13ce66" inactive-color="#ff4949"> </el-switch>
            </el-form-item>
          </el-form>
          <span slot="footer" class="dialog-footer">
            <el-button @click="dialogVisible = false">取 消</el-button>
            <el-button type="primary" @click="add">确 定</el-button>
          </span>
        </el-dialog>
        <el-alert :title="`数据一共${total}条`" type="info" show-icon :closable="false"> </el-alert>
        <div class="table">
          <el-table :data="list" style="width: 100%">
            <el-table-column type="index" label="序号"> </el-table-column>
            <el-table-column prop="subjectName" label="学科名称"> </el-table-column>
            <el-table-column prop="username" label="创建者"> </el-table-column>
            <el-table-column label="创建日期" width="160">
              <template slot-scope="scope">
                {{ scope.row.addDate | parseTimeByString }}
              </template>
            </el-table-column>
            <el-table-column label="前台是否显示">
              <template slot-scope="scope">
                {{ scope.row.isFrontDisplay | show }}
              </template>
            </el-table-column>
            <el-table-column prop="twoLevelDirectory" label="二级目录"> </el-table-column>
            <el-table-column prop="tags" label="标签"> </el-table-column>
            <el-table-column prop="totals" label="题目数量"> </el-table-column>
            <el-table-column label="操作" width="250">
              <template slot-scope="scope">
                <el-button type="text" size="small" @click="directorys">学科分类</el-button>
                <el-button type="text" size="small" @click="tags">学科标签</el-button>
                <el-button type="text" size="small" @click="change(scope.row)">修改</el-button>
                <el-button type="text" size="small" @click="del(scope.row)">删除</el-button>
              </template>
            </el-table-column>
          </el-table>
        </div>
        <div class="block">
          <el-pagination
            background
            :total="total"
            @size-change="handleSizeChange"
            @current-change="handleCurrentChange"
            :current-page="form.page"
            :page-sizes="[5, 10, 20, 50]"
            :page-size="form.pagesize"
            layout="prev, pager, next, sizes, jumper"
          >
          </el-pagination>
        </div>
      </el-card>
    </div>
  </div>
</template>

<script>
import { list, add, remove, update } from '@/api/hmmm/subjects.js'
export default {
  data () {
    return {
      input: '',
      dialogVisible: false,
      value: 0,
      list: [], // 从接口处取回
      form: {
        page: 1,
        pagesize: 10,
        subjectName: ''
      },
      subjectName: '',
      rules: {
        subjectName: [{ required: true, message: '请输入学科名称', trigger: 'blur' }]
      },
      total: 0,
      currentPage: 4
    }
  },
  created () {
    this.getlist()
  },
  methods: {
    handleClose (done) {
      this.$confirm('确认关闭？')
        .then(_ => {
          done()
        })
        .catch(_ => {})
    },
    async getlist () {
      const { data: res } = await list(this.form)
      console.log(res)
      this.list = res.items
      this.total = res.counts
    },
    handleSizeChange (size) {
      this.form.pagesize = size
      this.getlist()
    },
    handleCurrentChange (page) {
      this.form.page = page
      this.getlist()
    },
    directorys () {
      this.$router.push('/subjects/directorys')
    },
    tags () {
      this.$router.push('/subjects/tags')
    },
    add () {
      this.$refs.ruleForm.validate(async valid => {
        if (!valid) return this.$message.error('请填写必填项')
        if (this.form.subjectName) {
          await add({
            subjectName: this.form.subjectName,
            isFrontDisplay: this.value
          })
          this.$message.success('添加成功')
        } else {
          await update(this.form)
          this.$message.success('修改成功')
          this.form = {
            page: 1,
            pagesize: 10,
            subjectName: ''
          }
        }
        this.getlist()
        this.dialogVisible = false
      })
    },
    // 清空
    clear () {
      this.subjectName = ''
    },
    // 搜索
    async search () {
      const { data: res } = await list({ subjectName: this.subjectName })
      this.list = res.items
      this.total = res.counts
      this.clear()
    },
    // 删除
    del (row) {
      this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(async () => {
          await remove(row)
          this.$message.success('删除成功')
          this.getlist()
        })
        .catch(err => err)
    },
    // 修改
    change (row) {
      this.dialogVisible = true
      this.form = { ...row }
    }
  }
}
</script>

<style scoped>
.app-container {
  position: relative;
}
.span {
  font-weight: 700;
  margin-left: 15px;
}
.input {
  margin-left: 15px;
  width: 220px;
  height: 50px;
}
.button {
  margin-left: 10px;
}
.Dialog {
  position: absolute;
  right: 40px;
  top: 40px;
}
.input1 {
  margin-left: 10px;
  width: 240px;
  height: 50px;
}
.switch {
  margin-left: 10px;
}
.block {
  margin-top: 15px;
  margin-bottom: 15px;
  float: right;
}
</style>
